from django.shortcuts import render, redirect, reverse
from django.http import HttpResponse
from django.views.generic import View
from django.utils.decorators import method_decorator
from django.db.models import F
from .decorators import login_required
from . import forms
from .models import User


class IndexView(View):
    def get(self, request, *args, **kwargs):
        return render(request, 'index.html')


class SigninView(View):
    def get(self, request, *args, **kwargs):
        return render(request, 'signin.html')

    def post(self, request, *args, **kwargs):
        form = forms.SigninForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data.get('email')
            password = form.cleaned_data.get('password')
            user = User.objects.filter(email=email, password=password).first()
            if user:
                # TODO: 设置当前登录用户session信息
                request.session['user_id'] = user.id
                request.session.set_expiry(86400)
                # TODO: 登录成功重定向到首页
                return redirect(reverse('front:index'))
            else:
                # TODO: 账户或密码错误重定向到当前页面
                return redirect(reverse('front:signin'))
        else:
            print(form.errors.get_json_data())
            # TODO: 表单校验失败重定向到当前页面
            return redirect(reverse('front:signin'))


class SignupView(View):
    def get(self, request, *args, **kwargs):
        return render(request, 'signup.html')

    def post(self, request, *args, **kwargs):
        form = forms.SignupForm(request.POST)
        if form.is_valid():
            form.save()
            # TODO: 提交成功重定向到首页
            return redirect(reverse('front:index'))
        else:
            print(form.errors.get_json_data())
            # TODO: 表单校验失败重定向到当前页面
            return redirect(reverse('front:signup'))


@method_decorator([login_required], name='dispatch')
class TransferView(View):
    def get(self, request, *args, **kwargs):
        return render(request, 'transfer.html')

    def post(self, request, *args, **kwargs):
        form = forms.TransferForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data.get('email')
            money = form.cleaned_data.get('money')
            frontuser = request.frontuser
            if frontuser.banlance >= money:
                to_user = User.objects.filter(email=email)
                if to_user:
                    # TODO: 更新转账用户、当前用户余额
                    frontuser.banlance -= money
                    to_user.update(banlance=F('banlance')+money)
                    frontuser.save()
                    # TODO: 转账成功
                    return HttpResponse('转账成功')
                else:
                    print('对方账户不存在...')
                    # TODO: 重定向到当前页面
                    return redirect(reverse('front:transfer'))
            else:
                # TODO: 重定向到当前页面
                print('余额不足')
                return redirect(reverse('front:transfer'))
        else:
            # TODO: 表单校验失败重定向到当前页面
            print(form.errors.get_json_data())
            return redirect(reverse('front:transfer'))

    def dispatch(self, request, *args, **kwargs):
        return super(TransferView, self).dispatch(request, *args, **kwargs)


class LogoutView(View):
    def get(self, request, *args, **kwargs):
        # TODO: 清除当前这个用户的session数据
        request.session.clear()
        # TODO: 删除session并且删除在浏览器中存储的session_id，一般在注销的时候用得比较多
        request.session.flush()
        # TODO: 重定向到首页
        return redirect(reverse('front:index'))
